package tcp.test;

import java.io.*;
import java.net.Socket;

/*
客户端上传文件
 */
public class Client {
    public static void main(String[] args) throws Exception{
        Socket socket = new Socket("127.0.0.1",9999);
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        //转换为字符流,可以一次读一整行
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

        //写出文件名
        File file = new File("E:\\1.jpg");
        bw.write(file.getName());
        bw.newLine();
        bw.flush();


        String flag = br.readLine();
        if ("名字已读取".equals(flag)) {
            FileInputStream fis = new FileInputStream(file);
            //上传文件字节
            byte[] bys = new byte[1024];
            int len;
            while((len = fis.read(bys))!=-1){
                os.write(bys,0,len);
            }
            fis.close();
            //给服务端一个结束标记
            socket.shutdownOutput();
        }

        //读取反馈消息
        String result = br.readLine();
        System.out.println(result);
        socket.close();

    }
}
